module rank {

    export class BigRank extends egret.DisplayObjectContainer {

        private infos: Array<ScoreItem>;

        private rankItemGroup: egret.DisplayObjectContainer;

        private bgImg: egret.Bitmap;

        private btnPrevPage: PushButton;

        private btnNextPage: PushButton;

        public dispose(): void {
            if (this.items != null && this.items.length > 0) {
                while (this.items.length > 0) {
                    this.items.pop().dispose();
                }
            }
            this.items = null;
        }

        private btnsBox: egret.DisplayObjectContainer;

        private myRankItem: BigRankItem;

        public constructor() {
            super();
            this.bgImg = new egret.Bitmap(CommonRank.spriteSheet.getTexture("rank_bg"));
            this.addChild(this.bgImg);
            this.bgImg.visible = false;
            CommonRank.WIDTH = this.bgImg.width;
            CommonRank.HEIGHT = this.bgImg.height;


            let item: egret.Bitmap = new egret.Bitmap(CommonRank.spriteSheet.getTexture("rank_item"));
            item.visible = false
            BigRankItem.WIDTH = item.width;
            BigRankItem.HEIGHT = item.height;

            this.rankItemGroup = new egret.DisplayObjectContainer();
            this.addChild(this.rankItemGroup);
            this.rankItemGroup.y = 90;
            // this.rankItemGroup.y = 0;
            egret.log("CommonRank.WIDTH:", CommonRank.WIDTH);
            egret.log("BigRankItem.WIDTH:", BigRankItem.WIDTH);
            this.rankItemGroup.x = CommonRank.WIDTH - BigRankItem.WIDTH >> 1;

            this.btnsBox = new egret.DisplayObjectContainer();
            this.addChild(this.btnsBox);
        }

        public onPrevPage(eve: egret.Event=null): void {
            console.log('onPrevPage')
            if (this.pageIdx > 1) {
                this.pageIdx -= 1;
                this.layout();
            }
        }

        public onNextPage(eve: egret.Event=null): void {
            console.log('onNextPage')
            if (this.pageIdx < this.maxPageIdx) {
                this.pageIdx += 1;
                this.layout();
            }
        }

        private maxPageIdx: number = 1;
        public update(infos: Array<ScoreItem>): void {
            this.infos = infos;
            this.layout();
            egret.log("infos.length:", infos.length);
            if (infos.length > 0) {
                this.maxPageIdx = Math.ceil(infos.length / this.pageSize);
                for (let i: number = 0; i < infos.length; i++) {
                    if (infos[i].openid == Main.openid) {
                        this.myRankItem = new BigRankItem(infos[i], BigRankItem.INVISIBLE);
                        this.addChild(this.myRankItem);
                        this.myRankItem.x = CommonRank.WIDTH - BigRankItem.WIDTH >> 1;
                        this.myRankItem.y = CommonRank.HEIGHT - BigRankItem.HEIGHT;
                        break;
                    }
                }
            }
        }

        private items: Array<BigRankItem> = [];
        private pageIdx: number = 1;
        private pageSize: number = 6;

        private layout(): void {
            while (this.rankItemGroup.numChildren > 0) {
                helper.DisplayManager.remove(this.rankItemGroup.getChildAt(0));
            }

            let beginAt: number = (this.pageIdx - 1) * this.pageSize;
            let endAt: number = this.pageIdx * this.pageSize;
            for (let i = beginAt; i < endAt; i++) {
                let item: BigRankItem = new BigRankItem(this.infos[i], i % 2);
                this.rankItemGroup.addChild(item);
                item.y = (i - beginAt) * item.height;
                this.items.push(item);
            }
        }

    }

}